package com.tanhua.server.service;

import com.tanhua.manage.domain.db.UserInfo;
import com.tanhua.manage.domain.mongo.Comment;
import com.tanhua.manage.domain.vo.CommentVo;
import com.tanhua.manage.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.utils.RelativeDateFormat;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommentService {

    @Reference
    private CommentApi commentApi;
    @Autowired
    private RedisTemplate redisTemplate;
    @Reference
    private UserInfoApi userInfoApi;
    /**
     * 点赞功能
     */
    public long like(String publishId) {

        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setCommentType(1);//1-点赞
        comment.setUserId(loginUserId);
        comment.setTargetType(1);//1-对动态操作
        comment.setTargetId(new ObjectId(publishId));

        // 调用api保存评论, 获取返回的点赞数
        long likeCount = commentApi.save(comment);

        // 存入redis，标记当前用户对这个动态点过赞了
        String key = "publish_like_"+loginUserId+"_"+publishId;
        // 使用什么样的redis数据类型来替换key=value方式
        redisTemplate.opsForValue().set(key,1);
        // 返回的点赞数
        return likeCount;
    }

    /**
     * 取消点赞功能
     */
    public long dislike(String publishId) {
        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setCommentType(1);
        comment.setTargetUserId(loginUserId);
        comment.setTargetId(new ObjectId(publishId));

        long likeCount = commentApi.remove(comment);

        String key = "publish_like_"+loginUserId+"_"+publishId;
        redisTemplate.delete(key);

        return likeCount;
    }

    /**
     * 喜欢功能
     * @param publishId
     * @return
     */
    public long love(String publishId) {
        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setTargetId(new ObjectId(publishId));
        comment.setTargetUserId(loginUserId);
        comment.setCommentType(3);
        comment.setTargetType(1);

        long loveCount = commentApi.save(comment);

        String key ="publish_love_"+loginUserId+"_"+publishId;
        redisTemplate.opsForValue().set(key,1);



        return loveCount;
    }

    /**
     * 取消喜欢功能
     * @param publishId
     * @return
     */
    public long unlove(String publishId) {
        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setTargetId(new ObjectId(publishId));
        comment.setTargetUserId(loginUserId);
        comment.setCommentType(3);
        comment.setTargetType(1);

        long loveCount = commentApi.remove(comment);

        String key ="publish_love_"+loginUserId+"_"+publishId;
        redisTemplate.delete(key);



        return loveCount;
    }
    /**
     * 通过动态id查询评论列表
     */
    public PageResult<CommentVo> findPage(String publishId, long page, long pageSize) {
        PageResult pageResult = commentApi.findPage(publishId,page,pageSize);
        List<Comment> comments = pageResult.getItems();
        if (!CollectionUtils.isEmpty(comments)){

            List<Long> userIds = comments.stream().map(Comment::getUserId).collect(Collectors.toList());

            List<UserInfo> userInfoList = userInfoApi.findByBatchId(userIds);

            Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));

            List<CommentVo> voList = comments.stream().map(comment -> {
                CommentVo vo = new CommentVo();
                BeanUtils.copyProperties(comment, vo);
                vo.setId(comment.getId().toHexString());
                vo.setCreateDate(RelativeDateFormat.format(new Date(comment.getCreated())));

                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                BeanUtils.copyProperties(userInfo, vo);


                vo.setHasLiked(0);
                String key = "comment_like_" + UserHolder.getUserId() + "_" + vo.getId();
                if (redisTemplate.hasKey(key)) {
                    vo.setHasLiked(1);
                }

                return vo;
            }).collect(Collectors.toList());

            pageResult.setItems(voList);
        }

        return pageResult;
    }

    /**
     * 发表评论
     */
    public void add(Map<String, String> paramMap) {
        Long loginUserId = UserHolder.getUserId();
        String movementId = paramMap.get("movementId");
        String content = paramMap.get("comment");

        Comment comment = new Comment();
        comment.setUserId(loginUserId);
        comment.setTargetType(1);
        comment.setCommentType(2);
        comment.setTargetId(new ObjectId(movementId));
        comment.setContent(content);

        commentApi.save(comment);

    }
    /**
     * 点赞评论
     * @param commentId
     * @return
     */
    public Long likeComment(String commentId) {
        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setCommentType(1);
        comment.setTargetType(3);
        comment.setTargetId(new ObjectId(commentId));
        comment.setUserId(loginUserId);

        long likeCount = commentApi.likeComment(comment);

        String key = "comment_like_" + loginUserId+"_" + commentId;
        redisTemplate.opsForValue().set(key,1);

        return likeCount;
    }

    /**
     * 取消点赞评论
     * @param commentId
     * @return
     */
    public Long dislikeComment(String commentId) {
        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setCommentType(1);
        comment.setTargetType(3);
        comment.setTargetId(new ObjectId(commentId));
        comment.setUserId(loginUserId);

        long likeCount = commentApi.dislikeComment(comment);

        String key = "comment_like_" + loginUserId+"_" + commentId;
        redisTemplate.delete(key);

        return likeCount;
    }
}
